import List

quickSort:: (Ord a) => [a] -> [a]
quickSort [] = []
quickSort [x] = [x]
quickSort (x:xs) = quickSort([y| y <- xs, y<x]) ++ [x] ++ quickSort([y| y <- xs, y>x])

combinar:: (Ord a) => a -> [a] -> [[a]]
combinar x [] = [[x]]
combinar x (y:ys)
	| x >= y = combinar x ys
	| otherwise = [(x:(y:ys))] ++ combinar x ys

combinarInv:: (Ord a) => a -> [a] -> [[a]]
combinarInv x [] = [[x]]
combinarInv x (y:ys)
	| x <= y = combinarInv x ys
	| otherwise = [(x:(y:ys))] ++ combinarInv x ys

armarConjuntos:: (Ord a) => Int -> [a] -> [[a]]
armarConjuntos 0 xs = (combinar (xs!!0) xs) ++ [xs]
armarConjuntos (n+1) xs = (combinar (xs!!(n+1)) xs) ++ (armarConjuntos n xs) ++ (combinarInv (xs!!(n+1)) (reverse(xs)))-----

medio:: (Ord a) => [a] -> [[a]]
medio [] = [[]]
medio [x] = [[x]] 
medio (x:y:ys) = [init(y:ys)] ++ medio(init(y:ys))

primeros:: (Ord a) => [a] -> [[a]]
primeros [] = [[]]
primeros [x] = [[x]]
primeros xs = [init(xs)] ++ (primeros(init(xs)))

subconjuntos:: (Ord a) => [a] -> [[a]]
subconjuntos xs = nub(subconjuntosRec (length xs) xs)


subconjuntos2:: (Ord a) => [a] -> [[a]]
subconjuntos2 [] = [[]]
subconjuntos2 xs = nub (map (quickSort) (armarConjuntos ((length xs) - 1) (ys) ++ [[]] ++ medio(ys)) ++ primeros (ys)) -- iba adentro del parentesis
	where ys = quickSort(xs)

subconjuntosRec:: (Ord a) => Int -> [a] -> [[a]]
subconjuntosRec _ [] = [[]]
subconjuntosRec 0 xs = [xs]
subconjuntosRec (n+1) xs = (subconjuntos2 xs) ++ (subconjuntosRec n (take n xs))   
